﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using NazSoft.Framework;
using Nazsoft.Entity;
using Nazsoft.DAL;

//using Microsoft.Practices.EnterpriseLibrary.Data;
//using Com.CDN.BCL.Authentication;

[Serializable]
public class PageControl
{
    public int PageId { get; set; }
    public int AdminTypeId { get; set; }
}

public partial class AdminPermission : BasePage
{
    public List<PageControl> PageControl
    {
        get
        {
            var pageControls = ViewState["PageControl"] as List<PageControl>;
            if (pageControls == null)
            {
                pageControls = new List<PageControl>();
                ViewState["PageControl"] = pageControls;
            }
            return null;
        }

        set { ViewState["PageControl"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void AdminTypesView_SelectedIndexChanged(object sender, EventArgs e)
    {
        hdUserId.Value = AdminTypesView.SelectedRow.Cells[0].Text;

        if (Page.IsPostBack && hdUserId.Value != "")
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //gvAdminPermission.DataSource = AdminInfo.AdminPermissions(Convert.ToInt32(hdUserId.Value));
            //gvAdminPermission.DataBind();

            Panel1.Visible = true;
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {


        int adminType = string.IsNullOrEmpty(hdUserId.Value) ? 0 : Convert.ToInt32(hdUserId.Value);

        if (adminType != 0)
        {
            tblUserRights rights = new tblUserRights();
            rights.UserTypeId = adminType;
            tblUserRightsDAL dal = new tblUserRightsDAL();
            dal.Delete(rights);
            for (int i = 0; i < gvAdminPermission.Rows.Count; i++)
            {
                //HiddenField ChangedOrNo = gvAdminPermission.Rows[i].FindControl("ChangedOrNo") as HiddenField;
                var pageId = gvAdminPermission.Rows[i].FindControl("HddPageID") as HiddenField;
                var chkAll = gvAdminPermission.Rows[i].FindControl("ChkAll") as CheckBox;
                if (chkAll != null)
                    if (pageId != null)
                    {
                        if (chkAll.Checked)
                        {
                            rights.PageId = Convert.ToInt32(pageId.Value);
                            dal.Save(rights);
                        }
                    }
            }
        }
    }


    protected void AdminTypesView_SelectedIndexChanged1(object sender, EventArgs e)
    {
    }

    protected void gvAdminPermission_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        for (int i = 0; i < gvAdminPermission.Rows.Count; i++)
        {
            var pageId = gvAdminPermission.Rows[i].FindControl("HddPageID") as HiddenField;

            var chkAll = gvAdminPermission.Rows[i].FindControl("ChkAll") as CheckBox;
            int adminType = string.IsNullOrEmpty(hdUserId.Value) ? 0 : Convert.ToInt32(hdUserId.Value);
            if (adminType != 0)
            {
                if (chkAll != null)
                    if (chkAll.Checked)
                        if (pageId != null)
                            PageControl.Add(new PageControl { AdminTypeId = 0, PageId = Convert.ToInt32(pageId.Value) });
            }
        }
    }
    protected void AdminTypesView_SelectedIndexChanged2(object sender, EventArgs e)
    {

    }
    protected void AdminTypesView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        hdUserId.Value = (e.NewSelectedIndex + 1).ToString();//AdminTypesView.Rows[e.NewSelectedIndex].Cells[0].Text;
    }
}